/***************************************************************************
 *   This is a part of the NextCAD Web APP.                                *
 *   Copyright (C) AMCAX, All rights reserved.                             *
 ***************************************************************************/

import { CommandDialog, pickedFaceData, pickedLineData } from '@amcax/base';
import { CommandFormWrapper } from '@app-cad/common/components/command-form';
import { CommandFormFooter } from '@app-cad/common/components/command-form/footer';
import {
  MultiPickInput,
  PickInputValueType,
} from '@app-cad/common/components/pick-input/3d';
import { useCommand } from '@app-cad/common/hooks/useCommand';
import { ctx2 } from '@app-cad/module';
import { CurvedBaseCommand, CurvedCmdDialogParams } from '@app-cad/types';
import i18n from 'i18next';
import { useState } from 'react';
import { Vector3Tuple } from 'three-legacy';

type SurfaceInfo = {
  ncol: number;
  nrow: number;
  poles: Vector3Tuple[];
  udegree: number;
  vdegree: number;
};

type CurveInfo = {
  poles: Vector3Tuple[];
  degree: number;
};

function CommandSurfaceAnalyse({ title, onClose }: CurvedCmdDialogParams) {
  useCommand('cmd-surface-trim', {
    onClose,
  });

  const [picked, setPicked] = useState<pickedFaceData | pickedLineData>();
  const [curveInfo, setCurveInfo] = useState<CurveInfo>();
  const [surfaceInfo, setSurfaceInfo] = useState<SurfaceInfo>();

  return (
    <CommandDialog
      title={title}
      onClose={onClose}
      placement='topLeft'
    >
      <CommandFormWrapper>
        <MultiPickInput
          autoFocus
          value={[picked]}
          allowPickType={[PickInputValueType.Surface, PickInputValueType.Curve]}
          onPick={async ([result]) => {
            setPicked(result);

            if (result == null) {
              return;
            }

            if ((result as pickedLineData).edgeId != null) {
              setCurveInfo(
                await ctx2.commandCenters.curveCommands.getCurvePoles(
                  (result as pickedLineData).edgeId,
                ),
              );
              setSurfaceInfo(null);
            }

            if ((result as pickedFaceData).meshId != null) {
              setSurfaceInfo(
                await ctx2.commandCenters.surfaceCommands.getSurfacePoles(
                  (result as pickedFaceData).meshId,
                ),
              );
              setCurveInfo(null);
            }
          }}
          placeholder='请输入曲线/曲面'
        />
        <div>
          {surfaceInfo && (
            <>
              <div>
                阶数：{surfaceInfo.udegree} × {surfaceInfo.vdegree}
              </div>
              <div>控制点数量：{surfaceInfo.poles.length}</div>
            </>
          )}
          {curveInfo && (
            <>
              <div>阶数：{curveInfo.degree}</div>
              <div>控制点数量：{curveInfo.poles.length}</div>
            </>
          )}
        </div>
        <CommandFormFooter
          onClose={onClose}
          showSubmit={false}
        />
      </CommandFormWrapper>
    </CommandDialog>
  );
}

export const cmdAnalyse: CurvedBaseCommand = {
  id: 'CommandAnalyse',
  label: 'command.analyse',
  icon: 'commands/curved/analysis',
  create: (label, onClose, props) => (
    <CommandSurfaceAnalyse
      title={i18n.t(label)}
      onClose={onClose}
      props={props}
    />
  ),
};
